package com.magold.logic.region.airflow;

import java.util.LinkedList;
import java.util.List;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.particle.Particle;
import org.andengine.entity.particle.modifier.IParticleModifier;

import com.magold.algorithm.ShapeCollisionCheckerFix;
import com.magold.logic.LogicComponentFinder;
import com.magold.sprite.IMultiTextureSprite;


public class AirflowParticleRecycleHandler implements IParticleModifier<IMultiTextureSprite>, IParticleRecycleHandler<IMultiTextureSprite> {
	private List<IMultiTextureSprite> mDirtySprites;
	private Camera mCamera;
	
	public AirflowParticleRecycleHandler(Camera pCamera) {
		 mDirtySprites = new LinkedList<IMultiTextureSprite>();
		 mCamera = pCamera;
	}
	
	@Override
	public void onInitializeParticle(Particle<IMultiTextureSprite> pParticle) {
	}

	@Override
	public void onUpdateParticle(Particle<IMultiTextureSprite> pParticle) {
		IMultiTextureSprite sprite = pParticle.getEntity();
		
		if (sprite.getAlpha() < 0.01f || !ShapeCollisionCheckerFix.isVisible(mCamera, sprite)) {
			mDirtySprites.add(sprite);
		}
		
		if (mDirtySprites.contains(sprite)) {
			pParticle.setExpired(true);
			LogicComponentFinder.findConnector(sprite).setPhysicsEnabled(false);
			
			mDirtySprites.remove(sprite);
		}
		
	}
		
	@Override
	public void recycle(IMultiTextureSprite pSprite) {
		if (pSprite != null && !mDirtySprites.contains(pSprite)) {
			mDirtySprites.add(pSprite);
		}
	}
	
	

}
